package com.oracle.application.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oracle.application.entity.Member;
import com.oracle.application.service.IMemberService;
import com.oracle.application.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朱荣豪
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService iMemberService;
    @Autowired
    private JavaMailSender javaMailSender;

    @GetMapping("/getMemberByLoginName")
    public R<Member> getMemberByLoginName(@RequestParam("loginName") String loginName) {
        Member member = new Member();
        member.setLoginname(loginName);
        member.setModifydate(LocalDateTime.now());
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>(member);
        Member member1 = iMemberService.getOne(queryWrapper);
        return new R<>(200, "ok", member1);
    }

    @PostMapping("/addMember")
    public R<Member> addMember(@RequestBody Member member) {
        if (member.getEmail() == null || "".equals(member.getEmail())) {
            return new R<>(201, "邮箱不能为空", member);
        }
        if (member.getLoginname() == null || "".equals(member.getLoginname())) {
            return new R<>(201, "用户名不能为空", member);
        }
        if (member.getLoginpwd() == null || "".equals(member.getLoginpwd())) {
            return new R<>(201, "密码不能为空", member);
        }
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginName", member.getLoginname());
        if (this.iMemberService.getOne(queryWrapper) != null) {
            return new R<>(201, "用户名已存在", member);
        }
        member.setCreatedate(LocalDateTime.now());
        member.setModifydate(LocalDateTime.now());
        member.setStatus(2);
        addEmail(member.getEmail(), member.getLoginname());
        if (iMemberService.save(member)) {

            return new R<>(200, "注册成功，请去邮箱激活", member);
        }
        return new R<>(201, "注册失败，请稍后重试", member);
    }

    private void addEmail(String email, String loginName) {
        String id = DigestUtils.md5DigestAsHex(loginName.getBytes());
        String sendText = "<p>欢迎<" + loginName + ">用户,请在5分钟内点击下方链接激活：<a href='http://localhost:11002/member/checkEmail?loginName=" + loginName + "&email=" + email + "&id=" + id + "'>点击完成认证激活</a></p>";
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("XX系统验证码");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom("qq1263211102@163.com");
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        message.setTo(email);
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText(sendText);
        // 发送邮件
        javaMailSender.send(message);
    }

    @GetMapping("/checkEmail")
    public R<Member> checkEmail(@RequestParam("loginName") String loginName, @RequestParam("email") String email, @RequestParam("id") String id) {
        Member member = new Member();
        member.setLoginname(loginName);
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>(member);
        Member member1 = iMemberService.getOne(queryWrapper);
        if (member1 == null) {
            return new R<>(201, "用户不存在", null);
        }
        String idPro = DigestUtils.md5DigestAsHex(member1.getLoginname().getBytes());
        if (!member1.getEmail().equals(email)) {
            return new R<>(201, "邮箱错误", null);
        }
        if (!idPro.equals(id)) {
            return new R<>(201, "请求码错误", null);
        }
        LocalDateTime d1 = LocalDateTime.now();
        LocalDateTime d2 = member1.getCreatedate();
        Duration duration = Duration.between(d2, d1);
        long minutes = duration.toMinutes();//相差的分钟数
        if (minutes >= 5) {
            this.iMemberService.removeById(member1.getId());
            return new R<>(201, "链接已过期，请重新注册", null);
        }
        if (member1.getStatus() != 2) {
            return new R<>(201, "非法请求", null);
        }
        Member member2 = new Member();
        member2.setStatus(0);
        member2.setModifydate(LocalDateTime.now());
        QueryWrapper<Member> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", member1.getId());
        this.iMemberService.update(member2, queryWrapper2);
        return new R<>(200, "注册成功，请登录", null);
    }
}
